// pages/edit-user/index.js
import config from "../../config/index";
import { toPrevPage } from "../../utils/util";
import areaData from "../../utils/area.js";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    areaData: areaData,
    showAddress: false,
    address: {
      receiver_province: "",
      receiver_city: "",
      receiver_area: "",
    },
    sexs: [
      {
        value: "1",
        label: "男",
      },
      {
        value: "2",
        label: "女",
      },
    ],
  },

  showAddressSheet() {
    this.setData({
      showAddress: true,
    });
  },

  hideAddressSheet() {
    this.setData({
      showAddress: false,
    });
  },

  confirmAddress(event) {
    const { values } = event.detail;
    this.setData({
      "address.receiver_province": values[0].name,
      "address.receiver_city": values[1].name,
      "address.receiver_area": values[2].name,
      showAddress: false,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo();
  },

  getUserInfo() {
    wx.ajax.post("queryUserInfo.action", {}, true).then((res) => {
      if (res.success) {
        const result = res.result;

        this.setData({
          userInfo: res.result,
          address: {
            receiver_province: result.city01,
            receiver_city: result.city02,
            receiver_area: result.city03,
          },
        });
      }
    });
  },

  handleSexValueChange(e) {
    const { userInfo } = this.data;

    userInfo["sex"] = e.detail.value;

    this.setData({
      userInfo,
    });
  },

  handleSaveUserInfo() {
    const { userInfo, address } = this.data;

    if (!userInfo.head_img) {
      wx.showToast({
        title: "请上传头像",
        icon: "none",
        duration: 2000,
      });

      return;
    }

    if (!userInfo.name) {
      wx.showToast({
        title: "请输入姓名",
        icon: "none",
        duration: 2000,
      });

      return;
    }

    if (!userInfo.sex) {
      wx.showToast({
        title: "请选择性别",
        icon: "none",
        duration: 2000,
      });

      return;
    }

    if (!userInfo.phone) {
      wx.showToast({
        title: "请输入联系电话",
        icon: "none",
        duration: 2000,
      });

      return;
    }

    if (
      address &&
      (!address.receiver_province ||
        !address.receiver_city ||
        !address.receiver_area)
    ) {
      wx.showToast({
        title: "请选择地区",
        icon: "none",
        duration: 2000,
      });

      return;
    }

    if (!userInfo.detail_address) {
      wx.showToast({
        title: "请输入地址",
        icon: "none",
        duration: 2000,
      });
      return;
    }

    if (!userInfo.merchant) {
      wx.showToast({
        title: "请输入合作供应商",
        icon: "none",
        duration: 2000,
      });
      return;
    }

    const params = {
      name: userInfo.name,
      phone: userInfo.phone,
      head_img: userInfo.head_img,
      sex: userInfo.sex,
      city01: address.receiver_province,
      city02: address.receiver_city,
      city03: address.receiver_area,
      detail_address: userInfo.detail_address,
      merchant: userInfo.merchant
    };

    wx.ajax.post("editUserInfo.action", params, true).then((res) => {
      // console.log(res);
      if (res.success) {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });

        const timer = setTimeout(() => {
          toPrevPage();

          clearTimeout(timer);
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });
      }
    });
  },

  handleChangePhone(event) {
    const { value } = event.detail;
    const { userInfo } = this.data;

    userInfo["phone"] = value;

    this.setData({
      userInfo,
    });
  },

  handleEditName(event) {
    const { value } = event.detail;
    const { userInfo } = this.data;

    userInfo["name"] = value;

    this.setData({
      userInfo,
    });
  },

  handleChooseImage() {
    const _that = this;
    wx.chooseImage({
      count: 1,
      success: (res) => {
        // console.log(res);
        wx.uploadFile({
          url: `${config.uploadUrl}/ZXJC_FILE/fileUploadServlet`,
          filePath: res.tempFilePaths[0],
          name: "file",
          success: (r) => {
            // console.log(typeof r.data);
            const data = JSON.parse(r.data);
            if (data.success) {
              const result = data.result;
              const { userInfo } = _that.data;

              userInfo.head_img = result[0];

              _that.setData({ userInfo });
            }
          },
        });
      },
    });
  },

  handleEditDetailAddress(event) {
    const { value } = event.detail;
    const { userInfo } = this.data;

    userInfo["detail_address"] = value;

    this.setData({
      userInfo,
    });
  },

  handleEditMerchant(event) {
    const { value } = event.detail;
    const { userInfo } = this.data;

    userInfo["merchant"] = value;

    this.setData({
      userInfo,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
